package com.atguigu.day03;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Flink09_Transform_Max_MaxBy {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //2.从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);


        //3.使用map将从端口读进来的字符串转为JavaBean
        SingleOutputStreamOperator<WaterSensor> map = streamSource.map(new MapFunction<String, WaterSensor>() {

            @Override
            public WaterSensor map(String value) throws Exception {
                String[] split = value.split(",");
                return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
            }
        });

        //4.先对相同的id进行聚合
        KeyedStream<WaterSensor, Tuple> keyedStream = map.keyBy("id");

        //TODO 5.使用聚和算子
//        SingleOutputStreamOperator<WaterSensor> max = keyedStream.max("vc");
        SingleOutputStreamOperator<WaterSensor> min = keyedStream.min("vc");

//        SingleOutputStreamOperator<WaterSensor> maxBy = keyedStream.maxBy("vc", true);
        SingleOutputStreamOperator<WaterSensor> minBy = keyedStream.minBy("vc", true);

//        SingleOutputStreamOperator<WaterSensor> maxByFalse = keyedStream.maxBy("vc", false);
        SingleOutputStreamOperator<WaterSensor> minByFalse = keyedStream.minBy("vc", false);

//        max.print("max");
//        maxBy.print("maxBy");
//        maxByFalse.print("maxByFalse");

//        min.print("min");
//        minBy.print("minByTrue");
        minByFalse.print("minByFalse");
        env.execute();

    }
}
